package com.org.forex;

import java.io.IOException;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.DefaultHighLowDataset;

public class Statistic {
	private static Price[] prices;
	private static Price[] compare_prices;
	private static int size = 40;
	private static int start_year = 1999;
	private static int end_year = 2011;
	private static int idx = 0;
	private static int offset_sample = 100;
	private static String title = "Result";
	private static ResultWindow result_window;

	public static String getTitle() {
		return title;
	}

	public static void setTitle(String title) {
		Statistic.title = title;
	}

	public static int getOffset_sample() {
		return offset_sample;
	}

	public static void setOffset_sample(int offset_sample) {
		Statistic.offset_sample = offset_sample;
	}

	public static int getIndex() {
		return idx;
	}

	public static void setIndex(int idx) {
		Statistic.idx = idx;
	}

	public static int getSize() {
		return size;
	}

	public static void setSize(int size) {
		Statistic.size = size;
	}

	public static int getStart_year() {
		return start_year;
	}

	public static void setStart_year(int start_year) {
		Statistic.start_year = start_year;
	}

	public static int getEnd_year() {
		return end_year;
	}

	public static void setEnd_year(int end_year) {
		Statistic.end_year = end_year;
	}

	private static final String data_file = "C:\\e\\sonpt\\XAUUSD1440.csv";

	private static void loadPrice() {
		try {
			prices = DataUtils.loadCSVFile(data_file);
			System.out.println("data file load successfully");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static int min(int x1, int x2) {
		return x1 < x2 ? x1 : x2;
	}

	public static void show() {
		loadPrice();
		compare_prices = new Price[size];
		System.arraycopy(prices, prices.length - size - offset_sample,
				compare_prices, 0, size);
		int[] index = Price.getAllDiff(prices, compare_prices, 0, size, false,
				start_year, end_year,50);
		double diff_current = Price.diffOpen(prices, index[idx],
				compare_prices, 0, size)
				+ Price.diffClose(prices, index[idx], compare_prices, 0, size);

		double diff_next = Price.diffOpen(prices, index[idx] + size, prices,
				prices.length - offset_sample, size);

		System.out.println("mau thu " + idx);
		System.out.println("index : " + index[idx]);
		System.out.println("do lech hien tai " + diff_current);
		System.out.println("do lech tiep theo " + diff_next);

		boolean scaleday = false;

		// Draw sample chart
		final DefaultHighLowDataset sample_dataset = CandlestickDemo
				.createHighLowDataset(compare_prices, 0, size, scaleday);

		final JFreeChart sample_chart = CandlestickDemo
				.createChart(sample_dataset);
		sample_chart.setTitle("Sample chart");
		sample_chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);

		// Draw match chart
		final DefaultHighLowDataset match_dataset = CandlestickDemo
				.createHighLowDataset(prices, index[idx], index[idx] + size,
						scaleday);
		final JFreeChart match_chart = CandlestickDemo
				.createChart(match_dataset);
		match_chart.setTitle("Match chart");
		match_chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);

		// Draw next match chart

		final DefaultHighLowDataset next_dataset = CandlestickDemo
				.createHighLowDataset(prices, index[idx] + size,
						min(index[idx] + size + size, prices.length - 1),
						scaleday);
		final JFreeChart next_chart = CandlestickDemo.createChart(next_dataset);
		next_chart.setTitle("Next chart");
		next_chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);

		// Draw next sample chart

		final DefaultHighLowDataset sample_next_dataset = CandlestickDemo
				.createHighLowDataset(prices, prices.length - offset_sample,
						prices.length - offset_sample + size, scaleday);
		final JFreeChart sample_next_chart = CandlestickDemo
				.createChart(sample_next_dataset);
		sample_next_chart.setTitle("Sample next chart");
		sample_next_chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);

		// Draw the result window combine of all chart

		result_window = new ResultWindow(sample_chart, sample_next_chart,
				match_chart, next_chart, title + "  do lech hien tai "
						+ diff_current + " do lech tiep theo " + diff_next
						+ "index : " + index[idx]);
		result_window.show();

	}
}
